/*
//
//              INTEL CORPORATION PROPRIETARY INFORMATION
//  This software is supplied under the terms of a license  agreement or
//  nondisclosure agreement with Intel Corporation and may not be copied
//  or disclosed except in  accordance  with the terms of that agreement.
//    Copyright (c) 2007-2008 Intel Corporation. All Rights Reserved.
//
//
*/

#include "umc_defs.h"
#if defined(UMC_ENABLE_AVS_VIDEO_ENCODER)

#include <trace_log.h>
#include "umc_avs_enc_vlc.h"
#include "umc_avs_enc_bit_stream.h"

namespace UMC
{

const
Ipp32u VLCEnc_IntraMaxRun[12] =
{
    22, 14, 9, 6, 6, 4, 4, 4, 2, 2, 2, 1
};

const
Ipp32u VLCEnc_InterMaxRun[12] =
{
    25, 18, 13, 9, 6, 6, 6, 4, 4, 4, 3, 1
};

const
Ipp32u VLCEnc_ChromaMaxRun[6] =
{
    24, 19, 10, 7, 7, 4
};

const
AVS_ENC_ENTROPY_TABLE VLCEnc0_Intra =
{
    3,
    22,
    0, // I don't know EOB code for first table
    {
        0, 22, 38,
        2, 32, NA,
        4, 44, NA,
        6, 50, NA,
        8, 54, NA,
       10, NA, NA,
       12, NA, NA,
       14, NA, NA,
       16, NA, NA,
       18, NA, NA,
       20, NA, NA,
       24, NA, NA,
       26, NA, NA,
       28, NA, NA,
       30, NA, NA,
       34, NA, NA,
       36, NA, NA,
       40, NA, NA,
       42, NA, NA,
       46, NA, NA,
       48, NA, NA,
       52, NA, NA,
       56, NA, NA
    },
    {
        4,  3,  3,  3,
        3,  2,  2,  2,
        2,  2,  2,  2,
        2,  2,  2,  2,
        2,  2,  2,  2,
        2,  2,  2
    }
};

const
AVS_ENC_ENTROPY_TABLE VLCEnc1_Intra =
{
    6,
    14,
    8,
    {
        0,  4, 15, 27, 41, 55,
        2, 17, 35, NA, NA, NA,
        6, 25, 53, NA, NA, NA,
        9, 33, NA, NA, NA, NA,
       11, 39, NA, NA, NA, NA,
       13, 45, NA, NA, NA, NA,
       19, 49, NA, NA, NA, NA,
       21, 51, NA, NA, NA, NA,
       23, NA, NA, NA, NA, NA,
       29, NA, NA, NA, NA, NA,
       31, NA, NA, NA, NA, NA,
       37, NA, NA, NA, NA, NA,
       43, NA, NA, NA, NA, NA,
       47, NA, NA, NA, NA, NA,
       57, NA, NA, NA, NA, NA
    },
    {
        7,  4,  4,  3,
        3,  3,  3,  3,
        2,  2,  2,  2,
        2,  2,  2
    }
};

const
AVS_ENC_ENTROPY_TABLE VLCEnc2_Intra =
{
    9,
    9,
    8,
    {
        0,  2,  6, 13, 17, 27, 35, 45, 55,
        4, 11, 21, 33, 49, NA, NA, NA, NA,
        9, 23, 37, NA, NA, NA, NA, NA, NA,
       15, 29, 51, NA, NA, NA, NA, NA, NA,
       19, 39, NA, NA, NA, NA, NA, NA, NA,
       25, 43, NA, NA, NA, NA, NA, NA, NA,
       31, 53, NA, NA, NA, NA, NA, NA, NA,
       41, NA, NA, NA, NA, NA, NA, NA, NA,
       47, NA, NA, NA, NA, NA, NA, NA, NA,
       57, NA, NA, NA, NA, NA, NA, NA, NA
    },
    {
       10,  6,  4,  4,
        3,  3,  3,  2,
        2,  2
    }
};

const
AVS_ENC_ENTROPY_TABLE VLCEnc3_Intra =
{
    12,
    6,
    8,
    {
        0,  2,  4,  9, 11, 17, 21, 25, 33, 39, 45, 55,
        6, 13, 19, 29, 35, 47, NA, NA, NA, NA, NA, NA,
       15, 27, 41, 57, NA, NA, NA, NA, NA, NA, NA, NA,
       23, 37, 53, NA, NA, NA, NA, NA, NA, NA, NA, NA,
       31, 51, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
       43, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
       49, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
    },
    {
       13,  7,  5,  4,
        3,  2,  2
    }
};

const
AVS_ENC_ENTROPY_TABLE VLCEnc4_Intra =
{
    17,
    4,
    6,
    {
        0,  2,  4,  7,  9, 11, 15, 17, 21, 23, 29, 33, 35, 43, 47, 49, 57,
       13, 19, 27, 31, 37, 45, 55, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
       25, 41, 51, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
       39, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
       53, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
    },
    {
       18,  8,  4,  2,
        2
    }
};

const
AVS_ENC_ENTROPY_TABLE VLCEnc5_Intra =
{
    21,
    2,
    0,
    {
        1,  3,  5,  7,  9, 11, 13, 15, 17, 19, 23, 25, 27, 31, 33, 37, 41, 45, 49, 51, 55,
       21, 29, 35, 43, 47, 53, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
       39, 57, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
    },
    {
       22,  7,  3
    }
};

const
AVS_ENC_ENTROPY_TABLE VLCEnc6_Intra =
{
    26,
    1,
    0,
    {
        1,  3,  5,  7,  9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 35, 37, 39, 41, 43, 47, 49, 51, 53, 57,
       33, 45, 55, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
    },
    {
       27,  4
    }
};


const
AVS_ENC_ENTROPY_TABLE VLCEnc0_Inter =
{
    3,
    25,
    0,
    {
        0, 26, 40,
        2,  46, NA,
        4,  NA, NA,
        6,  NA, NA,
        8,  NA, NA,
        10, NA, NA,
        12, NA, NA,
        14, NA, NA,
        16, NA, NA,
        18, NA, NA,
        20, NA, NA,
        22, NA, NA,
        24, NA, NA,
        28, NA, NA,
        30, NA, NA,
        32, NA, NA,
        34, NA, NA,
        36, NA, NA,
        38, NA, NA,
        42, NA, NA,
        44, NA, NA,
        48, NA, NA,
        50, NA, NA,
        52, NA, NA,
        54, NA, NA,
        56, NA, NA
    },
    {
        4, 3, 2,2,2,2,2,2,  2,2,2,2,2,2, 2,2,2,2,2,2, 2,2,2,2,2,2
    }
};

const
AVS_ENC_ENTROPY_TABLE VLCEnc1_Inter =
{
    4,
    18,
    2,
    {
        0,  13, 29, 47,
        3,  23, 57, NA,
        5,  35, NA, NA,
        7,  39, NA, NA,
        9,  43, NA, NA,
        11, 49, NA, NA,
        15, 55, NA, NA,
        17, NA, NA, NA,
        19, NA, NA, NA,
        21, NA, NA, NA,
        25, NA, NA, NA,
        27, NA, NA, NA,
        31, NA, NA, NA,
        33, NA, NA, NA,
        37, NA, NA, NA,
        41, NA, NA, NA,
        45, NA, NA, NA,
        51, NA, NA, NA,
        53, NA, NA, NA
    },
    {
        5, 4, 3,3,3,3,3, 2,2,2,2,2, 2,2,2,2,2, 2,2
    }
};
const
AVS_ENC_ENTROPY_TABLE VLCEnc2_Inter =
{
    6,
    13,
    2,
    {
        0,   5, 11, 23, 35, 47,
        3,  13, 27, 49, NA, NA,
        7,  21, 45, NA, NA, NA,
        9,  29, 55, NA, NA, NA,
        15, 37, NA, NA, NA, NA,
        17, 41, NA, NA, NA, NA,
        19, 53, NA, NA, NA, NA,
        25, NA, NA, NA, NA, NA,
        31, NA, NA, NA, NA, NA,
        33, NA, NA, NA, NA, NA,
        39, NA, NA, NA, NA, NA,
        43, NA, NA, NA, NA, NA,
        51, NA, NA, NA, NA, NA,
        57, NA, NA, NA, NA, NA
    },
    {
        7, 5, 4, 4, 3,3,3, 2,2,2,2, 2,2,2
    }
};
const
AVS_ENC_ENTROPY_TABLE VLCEnc3_Inter =
{
    9,
    9,
    2,
    {
        0,   3,  7, 13, 17, 27, 35, 43, 55,
        5,  11, 21, 33, 51, NA, NA, NA, NA,
        9,  23, 37, 57, NA, NA, NA, NA, NA,
        15, 29, 47, NA, NA, NA, NA, NA, NA,
        19, 41, NA, NA, NA, NA, NA, NA, NA,
        25, 49, NA, NA, NA, NA, NA, NA, NA,
        31, NA, NA, NA, NA, NA, NA, NA, NA,
        39, NA, NA, NA, NA, NA, NA, NA, NA,
        45, NA, NA, NA, NA, NA, NA, NA, NA,
        53, NA, NA, NA, NA, NA, NA, NA, NA
    },
    {
        10, 6, 5, 4, 3,3, 2,2,2,2
    }
};
const
AVS_ENC_ENTROPY_TABLE VLCEnc4_Inter =
{
    12,
     6,
     2,
    {
         0,   3,  5,  9, 11, 17, 21, 25, 33, 41, 45, 55,
         7,  13, 19, 29, 35, 49, NA, NA, NA, NA, NA, NA,
         15, 27, 43, 57, NA, NA, NA, NA, NA, NA, NA, NA,
         23, 37, 51, NA, NA, NA, NA, NA, NA, NA, NA, NA,
         31, 53, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
         39, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
         47, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
    },
    {
        13, 7, 5, 4, 3, 2,2
    }
};
const
AVS_ENC_ENTROPY_TABLE VLCEnc5_Inter =
{
    16,
     4,
     0,
    {
         1,  3,  5,  7,  9, 13, 15, 17, 21, 25, 29, 33, 39, 43, 49, 53,
        11, 19, 27, 31, 41, 45, 57, NA, NA, NA, NA, NA, NA, NA, NA, NA,
        23, 37, 51, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
        35, 55, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
        47, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
    },
    {
         17, 8, 4, 3, 2
    }
};
const
AVS_ENC_ENTROPY_TABLE VLCEnc6_Inter =
{
    21,
     3,
     0,
    {
         1,  3,  5,  7,  9, 11, 13, 17, 19, 21, 23, 25, 29, 33, 35, 39, 41, 43, 47, 49, 57,
        15, 27, 37, 45, 55, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
        31, 51, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
        53, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
    },
    {
        22, 6, 3, 2
    }
};

const
AVS_ENC_ENTROPY_TABLE VLCEnc0_Chroma =
{
    4,
    24,
    0, // I do not know EOB code for this table
    {
        0, 14, 32, 56,
        2, 48, NA, NA,
        4, NA, NA, NA,
        6, NA, NA, NA,
        8, NA, NA, NA,
       10, NA, NA, NA,
       12, NA, NA, NA,
       16, NA, NA, NA,
       18, NA, NA, NA,
       20, NA, NA, NA,
       22, NA, NA, NA,
       24, NA, NA, NA,
       26, NA, NA, NA,
       28, NA, NA, NA,
       30, NA, NA, NA,
       34, NA, NA, NA,
       36, NA, NA, NA,
       38, NA, NA, NA,
       40, NA, NA, NA,
       42, NA, NA, NA,
       44, NA, NA, NA,
       46, NA, NA, NA,
       50, NA, NA, NA,
       52, NA, NA, NA,
       54, NA, NA, NA
    },
    {
        5,  3,  2,  2,
        2,  2,  2,  2,
        2,  2,  2,  2,
        2,  2,  2,  2,
        2,  2,  2,  2,
        2,  2,  2,  2,
        2
    }
};

const
AVS_ENC_ENTROPY_TABLE VLCEnc1_Chroma =
{
    5,
    19,
    0,
    {
        1,  5, 15, 29, 43,
        3, 21, 45, NA, NA,
        7, 37, NA, NA, NA,
        9, 41, NA, NA, NA,
       11, 53, NA, NA, NA,
       13, NA, NA, NA, NA,
       17, NA, NA, NA, NA,
       19, NA, NA, NA, NA,
       23, NA, NA, NA, NA,
       25, NA, NA, NA, NA,
       27, NA, NA, NA, NA,
       31, NA, NA, NA, NA,
       33, NA, NA, NA, NA,
       35, NA, NA, NA, NA,
       39, NA, NA, NA, NA,
       47, NA, NA, NA, NA,
       49, NA, NA, NA, NA,
       51, NA, NA, NA, NA,
       55, NA, NA, NA, NA,
       57, NA, NA, NA, NA
    },
    {
        6,  4,  3,  3,
        3,  2,  2,  2,
        2,  2,  2,  2,
        2,  2,  2,  2,
        2,  2,  2,  2
    }
};

const
AVS_ENC_ENTROPY_TABLE VLCEnc2_Chroma =
{
    9,
    10,
    2,
    {
        0,  3,  7, 11, 17, 27, 33, 47, 53,
        5, 13, 21, 37, 55, NA, NA, NA, NA,
        9, 23, 41, NA, NA, NA, NA, NA, NA,
       15, 31, 57, NA, NA, NA, NA, NA, NA,
       19, 43, NA, NA, NA, NA, NA, NA, NA,
       25, 45, NA, NA, NA, NA, NA, NA, NA,
       29, NA, NA, NA, NA, NA, NA, NA, NA,
       35, NA, NA, NA, NA, NA, NA, NA, NA,
       39, NA, NA, NA, NA, NA, NA, NA, NA,
       49, NA, NA, NA, NA, NA, NA, NA, NA,
       51, NA, NA, NA, NA, NA, NA, NA, NA
    },
    {
       10,  6,  4,  4,
        3,  3,  2,  2,
        2,  2,  2
    }
};

const
AVS_ENC_ENTROPY_TABLE VLCEnc3_Chroma =
{
    13,
    7,
    0,
    {
        1,  3,  5,  7, 11, 15, 19, 23, 29, 35, 43, 47, 53,
        9, 13, 21, 31, 39, 51, NA, NA, NA, NA, NA, NA, NA,
       17, 27, 37, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
       25, 41, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
       33, 55, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
       45, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
       49, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
       57, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
    },
    {
       14,  7,  4,  3,
        3,  2,  2,  2
    }
};

const
AVS_ENC_ENTROPY_TABLE VLCEnc4_Chroma =
{
    19,
    4,
    0,
    {
        1,  3,  5,  7,  9, 11, 13, 15, 19, 21, 23, 27, 29, 33, 37, 41, 43, 51, 55,
       17, 25, 31, 39, 45, 53, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
       35, 49, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
       47, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
       57, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
    },
    {
       20,  7,  3,  2,
        2
    }
};

} // namespace UMC

#endif // defined(UMC_ENABLE_AVS_VIDEO_ENCODER)
